<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>系部信息管理</title>
    <!-- Bootstrap Styles-->
    <link href="../../css/bootstrap/bootstrap.css" rel="stylesheet"/>
    <!-- FontAwesome Styles-->
    <link href="../../css/font-awesome.css" rel="stylesheet"/>
    <!-- Morris Chart Styles-->

    <!-- Custom Styles-->
    <link href="../../css/custom-styles.css" rel="stylesheet"/>
    <!-- Google Fonts-->
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'/>
    <!-- TABLE STYLES-->
    <link href="../../js/dataTables/dataTables.bootstrap.css" rel="stylesheet"/>
</head>

<body>
<div id="wrapper">
    <nav class="navbar navbar-default top-navbar" role="navigation">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".sidebar-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="../teacher/tea_course_index.html">题库与考试</a>
        </div>

        <ul class="nav navbar-top-links navbar-right">

            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
                    <i class="fa fa-user fa-fw"></i> <i class="fa fa-caret-down"></i>
                </a>
                <ul class="dropdown-menu dropdown-user">
                    <li>
                        <a href="../manager/adm_person_index.html"><i class="fa fa-user fa-fw"></i> 个人中心</a>
                    </li>
                    <li class="divider"></li>
                    <li>
                        <a href="../../login.html"><i class="fa fa-sign-out fa-fw"></i> 退出登录</a>
                    </li>
                </ul>
                <!-- /.dropdown-user -->
            </li>
            <!-- /.dropdown -->
        </ul>

    </nav>
    <!--/. NAV TOP  -->
    <nav class="navbar-default navbar-side" role="navigation">
        <div class="sidebar-collapse">
            <ul class="nav" id="main-menu">

                <li>
                    <a href="../manager/adm_index.html"><i class="fa fa-dashboard"></i> 用户角色管理</a>
                </li>
                <li>
                    <a href="../manager/adm_infoManage_index.html"><i class="fa fa-desktop"></i> 用户信息管理</a>
                </li>
                <li>
                    <a href="../manager/adm_schoolClass_index.html"><i class="fa fa-bar-chart-o"></i> 系部信息管理</a>
                </li>
                <li>
                    <a href="../manager/adm_exerciseType_index.html"><i class="fa fa-sitemap"></i> 试题类型管理</a>
                </li>
            </ul>

        </div>

    </nav>
    <!-- /. NAV SIDE  -->
    <div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <h1 class="page-header">
                        系部信息管理
                        <a href="#mymodal_3" role="button" data-toggle="modal">
                            <div style="display: inline;float: right;">
                                <button type="button" class="btn btn-primary">+新增学院</button>
                            </div>
                        </a>

                    </h1>
                </div>
            </div>
            <div class="well">
                <table class="table" id="school">
                    <thead>
                    <tr>
                        <th>#</th>
                        <th style="width: 180px;">学院名称</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
            <!-- 模态框1  -->

            <div class="modal fade" id="mymodal_1" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title" id="myModal_1Label">
                                修改信息
                            </h4>
                        </div>
                        <div class="modal-body">
                            <label for="">学院名称</label>
                            <input type="text" id="updateSchoolName" class="form-control">
                            <br>
                            <label for="">学院状态</label>
                            <select name="" id="updateSchoolState" class="form-control">
                                <option value="1">新建</option>
                                <option value="2">使用中</option>
                                <option value="3">不再使用</option>
                            </select>
                            <br>
                            <label for="">默认密码</label>
                            <input type="text" id="updateUndefaultPwd" class="form-control">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                            </button>
                            <button type="button" class="btn btn-primary" id="updateSchoolBtn">
                                提交更改
                            </button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal -->
            </div>
            <div class="modal fade" id="mymodal_2" tabindex="-1" role="dialog" aria-labelledby="myModelLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title" id="myModal_2Label">
                                是否删除?
                            </h4>
                        </div>
                        <div class="modal-body">
                            确定删除该学院？
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                            </button>
                            <button type="button" class="btn btn-danger" id="deleteBtn">
                                删除
                            </button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal -->
            </div>
            <!-- 模态框3 -->
            <div class="modal fade" id="mymodal_3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title" id="myModalLabel">
                                添加学院
                            </h4>
                        </div>
                        <div class="modal-body">
                            <input type="text" class="form-control" id="schoolName" placeholder="学院名称">
                            <br>
                            <input type="text" class="form-control" id="defaultPwd" placeholder="默认密码">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                            </button>
                            <button type="button" class="btn btn-primary" id="addSchool">
                                确认增加
                            </button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal -->
            </div>

            <div class="modal fade" id="mymodal_4" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title" id="departmentModalLabel">
                                系部信息管理
                            </h4>
                            <a href="#mymodal_5" role="button" data-toggle="modal">
                                <div style="display: inline;float: right;">
                                    <button type="button" class="btn btn-primary">+新增系部</button>
                                </div>
                            </a>
                        </div>
                        <div class="modal-body">
                            <table class="table" id="department">
                                <thead>
                                <tr>学院名称</tr>
                                <tr>操作</tr>
                                </thead>
                            </table>
                            <tbody>

                            </tbody>
                        </div>
                        <div class="modal-footer">
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal -->
            </div>

            <div class="modal fade" id="mymodal_5" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
                 aria-hidden="true">
                <div class="modal-dialog">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                                &times;
                            </button>
                            <h4 class="modal-title" id="">
                                增加系部
                            </h4>
                        </div>
                        <div class="modal-body">
                            <label for="">系部名称</label>
                            <input type="text" class="form-control" id="departmentName">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                            </button>
                            <button type="button" class="btn btn-primary" id="addDepartment">
                                确认增加
                            </button>
                        </div>
                    </div>
                    <!-- /.modal-content -->
                </div>
                <!-- /.modal -->
            </div>

            <!-- /. PAGE WRAPPER  -->
            <!-- /. WRAPPER  -->
            <!-- JS Scripts-->
            <!-- jQuery Js -->
            <script src="../../js/jquery-1.10.2.js"></script>
            <!-- Bootstrap Js -->
            <script src="../../js/bootstrap.min.js"></script>
            <!-- Metis Menu Js -->
            <script src="../../js/jquery.metisMenu.js"></script>
            <!-- DATA TABLE SCRIPTS -->
            <script src="../../js/dataTables/jquery.dataTables.js"></script>
            <script src="../../js/dataTables/dataTables.bootstrap.js"></script>
            <script src="../../js/jquerysession.js"></script>
            <script>
                var schoolid = 0;
                $(document).ready(function () {
                    $('#dataTables-example').dataTable();
                    if ($.session.get('userid') == null) {
                        alert("请先登录！！！");
                        window.location.href = '/login.html';
                    } else {
                        if ($.session.get('usertype') != 3) {
                            alert("您没有访问这个页面的权限！！！");
                            window.location.href = '/login.html';
                        }
                    }

                    //加载所有的学院
                    $.ajax({
                        type:'post',
                        url:'/school/findAll',
                        dataType:'json',
                        success:function (data) {
                            $.each(data.data,function (index,obj) {
                                $("#school").append('<tr><td>'+obj.id+'</td><td>'+obj.name+'</td><td>' +
                                    '<button data-target="#mymodal_1" class="btn btn-primary" data-toggle="modal" onclick="Values('+obj.id+');schoolMes('+obj.id+');">修改 </button>' +
                                    '<button data-target="#mymodal_2" data-toggle="modal" class="btn btn-danger" onclick="Values('+obj.id+')">删除</button>' +
                                    '<button data-target="#mymodal_4" data-toggle="modal" class="btn btn-primary" onclick="getDepartment('+obj.id+')">查看系部</button>' +
                                    '</td></tr>');
                            });
                        }
                    })
                });

                function Values(ID){
                    schoolid = ID;
                }
                //增加学院
                $("#addSchool").click(function (e) {
                    e.preventDefault();
                    var schoolName = $("#schoolName").val();
                    var defaultPwd = $("#defaultPwd").val();
                    if (schoolName == "" || defaultPwd == ""){
                        alert("填入必要信息");
                    }else{
                        var school = JSON.stringify({"name":schoolName,"state":2,"userdefaultpwd":defaultPwd});
                        $.ajax({
                            type:'post',
                            url:'/school/addOne',
                            data:school,
                            contentType:'application/json',
                            dataType:'json',
                            success:function (data) {
                                if (data.msg=="success"){
                                    alert("添加成功");
                                } else{
                                    alert(data.msg);
                                }
                                window.location.href="adm_schoolClass_index.html";
                            }
                        })
                    }
                });

                //删除学院
                $("#deleteBtn").click(function (e) {
                    e.preventDefault();
                    $.ajax({
                        type:'get',
                        url:'/school/delete/'+schoolid,
                        dataType:'json',
                        success:function (data) {
                            if (data.msg == "success"){
                                alert("删除成功");
                            }else{
                                alert("删除失败");
                            }
                            window.location.href="adm_schoolClass_index.html";
                        }
                    })
                });

                //更新学院
                $("#updateSchoolBtn").click(function (e) {
                    e.preventDefault();
                    var schoolname = $("#updateSchoolName").val();
                    var schoolState = $("#updateSchoolState").val();
                    var undefaultpwd = $("#updateUndefaultPwd").val();
                    
                    if (schoolname == "" || undefaultpwd == ""){
                        alert("请填入必要的参数");
                    }else{
                        var updateSchool = JSON.stringify({"id":schoolid,"name":schoolname,"status":schoolState,"userdefaultpwd":undefaultpwd});
                        $.ajax({
                            type:'post',
                            url:'/school/updateBySchoolId',
                            data:updateSchool,
                            contentType:'application/json',
                            dataType:'json',
                            success:function (data) {
                                if (data.msg == "success"){
                                    alert("修改成功");
                                }else{
                                    alert(data.msg);
                                }
                                window.location.href="adm_schoolClass_index.html";
                            }
                        })
                    }
                });

                // 添加系部
                $("#addDepartment").click(function (e) {
                   e.preventDefault();
                   var departmentName = $('#departmentName').val();
                   if (departmentName == ""){
                       alert("请输入必要的信息");
                   }else{
                       var department = JSON.stringify({"name":departmentName,"schoolid":schoolid});
                       $.ajax({
                           type:'post',
                           url:'/Department/add',
                           data:department,
                           contentType:'application/json',
                           dataType:'json',
                           success:function (data) {
                               if (data.msg == "success"){
                                   alert("添加成功");
                               }else{
                                   alert(data.msg);
                               }
                               window.location.href="adm_schoolClass_index.html";
                           }
                       })
                   }
                });
                //修改学院的信息
                function schoolMes(id){
                    $.ajax({
                        typr:'get',
                        url:'/school/findBySchoolId/'+id,
                        dataType:'json',
                        success:function (data) {
                            if (data.msg=="success"){
                                console.log(data);
                                $("#updateSchoolName").val(data.data.name);
                                $("#updateSchoolState").val(data.data.status);
                                $("#updateUndefaultPwd").val(data.data.userdefaultpwd);
                            }
                        }
                    })
                }

                function getDepartment(id){
                    schoolid = id;
                    $.ajax({
                        type:'get',
                        url:'/Department/findBySchoolid/'+id,
                        dataType:'json',
                        success:function (data) {
                            $.each(data.data,function (index,obj) {
                                $("#department").append('<tr><td>'+obj.name+'</td><td>' +
                                    '<button data-target="#mymodal_5" data-toggle="modal" class="btn btn-danger" onclick="deleteDep('+obj.id+')">删除</button>' +
                                    '</td></tr>');
                            })

                        }
                    })
                }

                // 删除系部
                function deleteDep(depId){
                    $.ajax({
                        type:'get',
                        url:'/Department/deleteById/'+depId,
                        data:'json',
                        success:function (data) {
                            if (data.msg == "success"){
                                alert("删除成功");
                            }else{
                                alert(data.msg);
                            }
                            window.location.href="adm_schoolClass_index.html";

                        }
                    })
                }


            </script>
        </div>
    </div>
</div>
</body>

</html>